The problem can be found at the following link: Question Link
We can utilize an additional stack to temporarily store the elements while popping until we reach the middle element in the original stack. Here are the steps involved:
- Determine the number of elements on top of the middle element by dividing the
sizeOfStack
by 2, and assign the result to the variablecnt
. - Iterate
cnt
times and temporarily store the elements in thenewStack
. - Pop the middle element from the original stack
s
. - Re-push the elements stored in the
newStack
back into the original stacks
.
- Time Complexity:
O(N)
, whereN
is the number of elements in the stack. - Auxiliary Space Complexity:
O(N)
, as we are using an auxiliary stack to temporarily store elements.
class Solution {
public:
void deleteMid(stack<int>& s, int sizeOfStack) {
stack<int> newStack;
int cnt = sizeOfStack / 2; // number of elements present on the top of mid element
while (cnt--) {
newStack.push(s.top());
s.pop();
}
s.pop(); // Mid Element
while (!newStack.empty()) {
s.push(newStack.top());
newStack.pop();
}
}
};
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.